﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using Starksoft.Net.Ftp;
using WcfService1.DTO;
using TransferMode = Starksoft.Net.Ftp.TransferMode;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MailServices" in code, svc and config file together.

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class MailServices : IMailServices
    {
       private WcfSessionStorage _session;
        private PreSalesOrderRepository preSalesOrderRepository;
        private BTBRepository btbRepository;
        private DeliveryOrderRepository DORepository;
        private BtbElseRepository btbElseRepository;
        private MutasiWarehouseRepository mutasiWarehouseRepository;
        private InventoryAdjustmentRepository inventoryAdjustRepository;
        private PurchaseInvoiceRepository piRepository;
        private PurchaseOrderRepository poRepository;
        private PurchaseReturnRepository prRepository;
        private SalesOrderRepository soRepository;
        private SpkRepository spkRepository;
        private SalesInvoiceRepository siRepository;
        private SalesReturnRepository srRepository;
        private AppParamRepository paramRepository;
        private ItemRepository itemRepository;
        private SubGroupRepository subGroupRepository;
        private GroupRepository groupRepository;


        public MailServices()
        {
            _session = new WcfSessionStorage();
            preSalesOrderRepository = new PreSalesOrderRepository(_session);
            btbRepository = new BTBRepository(_session);
            DORepository = new DeliveryOrderRepository(_session);
            btbElseRepository = new BtbElseRepository(_session);
            mutasiWarehouseRepository = new MutasiWarehouseRepository(_session);
            inventoryAdjustRepository = new InventoryAdjustmentRepository(_session);
            piRepository = new PurchaseInvoiceRepository(_session);
            poRepository = new PurchaseOrderRepository(_session);
            prRepository = new PurchaseReturnRepository(_session);
            soRepository = new SalesOrderRepository(_session);
            spkRepository = new SpkRepository(_session);
            siRepository = new SalesInvoiceRepository(_session);
            srRepository = new SalesReturnRepository(_session);
            paramRepository = new AppParamRepository(_session);
            itemRepository = new ItemRepository(_session);
            subGroupRepository = new SubGroupRepository(_session);
            groupRepository = new GroupRepository(_session);
        }

        
        public void DoWork()
        {
            // Add your operation implementation here
            return;
        }


       
        public bool SendEmail(Object param,String DestinationFolder)
        {
            bool res = true;
            string Subject = string.Empty;
            string path = AppDomain.CurrentDomain.BaseDirectory + "/bin/Outbox/";
            string remotePath = string.Empty;
            Action UpdateEmailStatus = default(Action);

            switch (param.GetType().Name)
            {
                case "PreSalesOrderDTO":
                    path = path + "PreSalesOrder/";
                    (param as PreSalesOrderDTO).WriteObject(path);
                    Subject = (param as PreSalesOrderDTO).PSNo;
                    remotePath = "PreSalesOrderDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                                            {
                                                preSalesOrderRepository.UpdateEmailStatus(Subject);
                                            };
                    break;
                case "SalesOrderDTO":
                    path = path + "SalesOrder/";
                    (param as SalesOrderDTO).WriteObject(path);
                    Subject = (param as SalesOrderDTO).SONo;
                    remotePath = "SalesOrderDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        soRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "BTBDTO":
                    path = path + "BTB/";
                    (param as BTBDTO).WriteObject(path);
                    Subject = (param as BTBDTO).NoBtb;
                    remotePath = "BTBDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        btbRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "BtbElseDTO":
                    path = path + "BTBElse/";
                    (param as BtbElseDTO).WriteObject(path);
                    Subject = (param as BtbElseDTO).NoBtb;
                    remotePath = "BtbElseDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        btbElseRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "DeliveryOrderDTO":
                    path = path + "DeliveryOrder/";
                    (param as DeliveryOrderDTO).WriteObject(path);
                    Subject = (param as DeliveryOrderDTO).DONo;
                    remotePath = "DeliveryOrderDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        DORepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "MutasiPalletDTO":
                    path = path + "MutasiPallet/";
                    (param as MutasiPalletDTO).WriteObject(path);
                    Subject = (param as MutasiPalletDTO).NoMutasi;
                    remotePath = "MutasiPalletDTO;" + Subject.Replace("/", "") + ".xml";
                    break;
                case "MutasiWarehouseDTO":
                    path = path + "MutasiWarehouse/";
                    (param as MutasiWarehouseDTO).WriteObject(path);
                    Subject = (param as MutasiWarehouseDTO).NoMutasi;
                    remotePath = "MutasiWarehouseDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        mutasiWarehouseRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "InventoryAdjustmentDTO":
                    path = path + "InventoryAdjustment/";
                    (param as InventoryAdjustmentDTO).WriteObject(path);
                    Subject = (param as InventoryAdjustmentDTO).NoAdj;
                    remotePath = "InventoryAdjustmentDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        inventoryAdjustRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "PurchaseReturnDTO":
                    path = path + "PurchaseReturn/";
                    (param as PurchaseReturnDTO).WriteObject(path);
                    Subject = (param as PurchaseReturnDTO).PRNo;
                    remotePath = "PurchaseReturnDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        prRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "SalesReturnDTO":
                    path = path + "SalesReturn/";
                    (param as SalesReturnDTO).WriteObject(path);
                    Subject = (param as SalesReturnDTO).SRNo;
                    remotePath = "SalesReturnDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        srRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "PurchaseOrderDTO":
                    path = path + "PurchaseOrder/";
                    (param as PurchaseOrderDTO).WriteObject(path);
                    Subject = (param as PurchaseOrderDTO).PONumber;
                    remotePath = "PurchaseOrderDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        poRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "SpkDTO":
                    path = path + "SPK/";
                    (param as SpkDTO).WriteObject(path);
                    Subject = (param as SpkDTO).NoSPK;
                    remotePath = "SpkDTO;" + Subject.Replace("/", "") + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        spkRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "ItemDTO":
                    path = path + "Item/";
                    (param as ItemDTO).WriteObject(path);
                    Subject = (param as ItemDTO).ItemId;
                    remotePath = "ItemDTO;" + Subject + ".xml";
                    UpdateEmailStatus = () =>
                    {
                       itemRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "SubGroupDTO":
                    path = path + "Subgroup/";
                    (param as SubGroupDTO).WriteObject(path);
                    Subject = (param as SubGroupDTO).SubGroupID;
                    remotePath = "SubGroupDTO;" + Subject + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        subGroupRepository.UpdateEmailStatus(Subject);
                    };
                    break;
                case "GroupDTO":
                    path = path + "Group/";
                    (param as GroupDTO).WriteObject(path);
                    Subject = (param as GroupDTO).GroupID;
                    remotePath = "GroupDTO;" + Subject + ".xml";
                    UpdateEmailStatus = () =>
                    {
                        groupRepository.UpdateEmailStatus(Subject);
                    };
                    break;
            }
            path = path + param.GetType().Name + ".xml";
            var ftp = new FtpClient("118.96.52.189", 21);

            try
            {
                ftp.FileTransferType = TransferType.Binary;
                ftp.DataTransferMode = TransferMode.Passive;
                ftp.Open("oka", "brucelee");
                ftp.ChangeDirectory("Database");
                ftp.ChangeDirectory(DestinationFolder);
                //FtpItemCollection col = ftp.GetDirList();
                ftp.PutFile(path, remotePath, FileAction.Create);
                bool isCentral = false;
                var central = paramRepository.GetDataOne(x => x.AppParamName == "Central").AppParamValue;
                var gudang = paramRepository.GetDataOne(x => x.AppParamName == "Gudang").AppParamValue;
                
                if (central.ToLower() == gudang.ToLower())
                        isCentral = true;

                if (! isCentral && DestinationFolder.ToLower() != central.ToLower()) //Kalo bukan Pusat(Cabang) kirim ke Pusat juga
                {
                    UpdateEmailStatus();
                    ftp.ChangeDirectoryUp();
                    ftp.ChangeDirectory("Database");
                    ftp.ChangeDirectory(central);
                    ftp.PutFile(path, remotePath, FileAction.Create);
                }
               
                
            }
            catch (Exception ex)
            {
                res = false;
            }finally
            {
                ftp.Close();
                UpdateEmailStatus = null;
            }
            return res;
        }

       
        public IList<String> ReadMail(string Username, string Password,string KodeWarehouse, out string errmessage)
        {
            errmessage = string.Empty;
            var Results = new List<string>();
            var di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/bin/Inbox/");
            var ftp = new FtpClient("118.96.52.189", 21);
            try
            {
                
                ftp.FileTransferType = TransferType.Binary;
                ftp.DataTransferMode = TransferMode.Passive;
                ftp.Open("oka", "brucelee");
                ftp.ChangeDirectory("Database");
                ftp.ChangeDirectory(KodeWarehouse);
                FtpItemCollection col = ftp.GetDirList();

                foreach (FtpItem item in col)
                {
                    Results.Add(item.Name);
                }

            }catch (Exception ex)
            {
                errmessage = ex.Message;
            }finally
            {
                if (ftp.IsConnected)
                        ftp.Close();
            }

          
            return Results;
        }


       
        public void ReceiveAll(out string err,string KodeWarehouse)
        {
             err = string.Empty;
             var ftp = new FtpClient("118.96.52.189", 21);
            try
            {
                ftp.FileTransferType = TransferType.Binary;
                ftp.DataTransferMode = TransferMode.Passive;
                ftp.Open("oka", "brucelee");
                ftp.ChangeDirectory("Database");
                ftp.ChangeDirectory(KodeWarehouse);
                FtpItemCollection col = ftp.GetDirList();

                foreach (FtpItem item in col)
                {
                    string localpath = AppDomain.CurrentDomain.BaseDirectory + @"\bin\Inbox\" + item.Name;
                    localpath = localpath.Replace(";", "_");
                    ftp.GetFile(item.Name, localpath, FileAction.Create);
                    var subject = item.Name.Split(new string[] {";"}, StringSplitOptions.RemoveEmptyEntries);
                    switch (subject.FirstOrDefault())
                    {
                        case "PreSalesOrderDTO":
                            var PreSales = new PreSalesOrderDTO();
                            PreSales = PreSales.ReadObject(localpath);
                            var preSalesOrderServices = new PreSalesOrderServices();
                            var PS = preSalesOrderRepository.GetDataOne(x => x.PSNo == PreSales.PSNo);
                            if (PS == null)
                            {
                                preSalesOrderServices.Save(PreSales);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "BTBDTO":
                            var btb = new BTBDTO();
                            btb = btb.ReadObject(localpath);
                            var btbServices = new BTBServices();
                            var BTB = btbRepository.GetDataOne(x => x.NoBtb == btb.NoBtb);
                            if (BTB==null)
                            {
                                btbServices.Save(btb);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "BtbElseDTO":
                             var btbElse = new BtbElseDTO();
                             btbElse = btbElse.ReadObject(localpath);
                            var btbElseServices = new BtbElseServices();
                            var BTBElse = btbElseRepository.GetDataOne(x => x.NoBtb == btbElse.NoBtb);
                            if (BTBElse == null)
                            {
                                btbElseServices.Save(btbElse);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "DeliveryOrderDTO":
                            var DO = new DeliveryOrderDTO();
                            DO = DO.ReadObject(localpath);
                            var deliveryOrderServices = new DeliveryOrderServices();
                            var deliveryOrder = DORepository.GetDataOne(x => x.DONo == DO.DONo);
                            if (deliveryOrder == null )
                            {
                                deliveryOrderServices.Save(DO);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "MutasiPalletDTO":
                            //var MutasiPallet = new MutasiPalletDTO();
                            //MutasiPallet = MutasiPallet.ReadObject(localpath);
                            //var mutasiPalletServices = new MutasiPalletServices();
                            //mutasiPalletServices.Save(MutasiPallet);
                            //break;
                        case "MutasiWarehouseDTO":
                            var mutasiWarehouse = new MutasiWarehouseDTO();
                            mutasiWarehouse = mutasiWarehouse.ReadObject(localpath);
                            var mutasiWarehouseServices = new MutasiWarehouseServices();
                            var mw = mutasiWarehouseRepository.GetDataOne(x => x.NoMutasi == mutasiWarehouse.NoMutasi);
                            if (mw == null)
                            {
                                mutasiWarehouseServices.Save(mutasiWarehouse);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "InventoryAdjustmentDTO":
                            var inventoryAdjustment = new InventoryAdjustmentDTO();
                            inventoryAdjustment = inventoryAdjustment.ReadObject(localpath);
                            var inventoryAdjustmentServices = new InventoryAdjustmentServices();
                            var invAdjust =
                                inventoryAdjustRepository.GetDataOne(x => x.NoAdj == inventoryAdjustment.NoAdj);
                            if (invAdjust == null)
                            {
                                inventoryAdjustmentServices.Save(inventoryAdjustment);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "PurchaseReturnDTO":
                            var PR = new PurchaseReturnDTO();
                            PR = PR.ReadObject(localpath);
                            var purchaseReturnServices = new PurchaseReturnServices();  
                            var purchaseReturn = prRepository.GetDataOne(x => x.PRNo == PR.PRNo);
                            if (purchaseReturn == null)
                            {
                                purchaseReturnServices.Save(PR);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "SalesReturnDTO":
                            var SR = new SalesReturnDTO();
                            SR = SR.ReadObject(localpath);
                            var salesReturnServices = new SalesReturnServices();
                            var salesReturn = srRepository.GetDataOne(x => x.SRNo == SR.SRNo);
                            if (salesReturn == null)
                            {
                                salesReturnServices.Save(SR);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "PurchaseOrderDTO":
                            var PO = new PurchaseOrderDTO();
                            PO = PO.ReadObject(localpath);
                            var purchaseOrderServices = new PurchaseOrderServices();
                            var purchaseOrder = poRepository.GetDataOne(x => x.PONumber == PO.PONumber);
                            if (purchaseOrder == null)
                            {
                                purchaseOrderServices.Save(PO);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "SalesOrderDTO":
                            var SO = new SalesOrderDTO();
                            SO = SO.ReadObject(localpath);
                            var salesOrderServices = new SalesOrderServices();
                            var salesOrder = soRepository.GetDataOne(x => x.SONo == SO.SONo);
                            if (salesOrder == null)
                            {
                                salesOrderServices.Save(SO);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "SpkDTO":
                            var SPK = new SpkDTO();
                            SPK = SPK.ReadObject(localpath);
                            var  spkServices = new SpkServices();
                            var spk = spkRepository.GetDataOne(x => x.NoSPK == SPK.NoSPK);
                            if (spk == null)
                            {
                                spkServices.Save(SPK);
                                ftp.DeleteFile(item.Name);
                            }
                            break;
                        case "ItemDTO":
                            var itemDto = new ItemDTO();
                            itemDto = itemDto.ReadObject(localpath);
                            var ItemServices = new ItemServices();
                            ItemServices.Save(itemDto);
                            ftp.DeleteFile(item.Name);
                            break;
                        case "SubGroupDTO":
                            var subGroupDto = new SubGroupDTO();
                            subGroupDto = subGroupDto.ReadObject(localpath);
                            var SubgroupServices = new SubGroupService();
                            SubgroupServices.Save(subGroupDto);
                            ftp.DeleteFile(item.Name);
                            break;
                        case "GroupDTO":
                            var groupDto = new GroupDTO();
                            groupDto = groupDto.ReadObject(localpath);
                            var GroupServices = new GroupServices();
                            GroupServices.Save(groupDto);
                            ftp.DeleteFile(item.Name);
                            break;
                    }
                    FileInfo fi = new FileInfo(localpath);
                    fi.Delete();
                }
                
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }finally
            {
                ftp.Close();
            }
        }


       
        public String Receive(string tipeTransaksi,String KodeWarehouse,out string path) 
        {
            string Result = string.Empty;
            path = string.Empty;
            var ftp = new FtpClient("118.96.52.189", 21);
            try
            {
               
                ftp.FileTransferType = TransferType.Binary;
                ftp.DataTransferMode = TransferMode.Passive;
                ftp.Open("oka", "brucelee");
                ftp.ChangeDirectory("Database");
                ftp.ChangeDirectory(KodeWarehouse);
                FtpItemCollection col = ftp.GetDirList();

             

                foreach (FtpItem item in col)
                {
                    string localpath = AppDomain.CurrentDomain.BaseDirectory + @"\bin\Inbox\" + item.Name;
                    localpath = localpath.Replace(";", "_");
                    var subject = item.Name.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                    if (subject.FirstOrDefault() != tipeTransaksi) continue;

              
                      ftp.GetFile(item.Name, localpath, FileAction.Create);
                      path = localpath;
                      Result = subject.FirstOrDefault();
                                                         //switch (subject.FirstOrDefault())
                                                         //{
                                                         //    case "PreSalesOrderDTO":
                                                         //        var PreSales = new PreSalesOrderDTO();
                                                         //        PreSales = PreSales.ReadObject(localpath);
                                                         //        var preSalesOrderServices = new PreSalesOrderServices();
                                                         //        var PS = preSalesOrderRepository.GetDataOne(x => x.PSNo == PreSales.PSNo);
                                                         //        //if (PS == null)
                                                         //        //{
                                                         //        preSalesOrderServices.Save(PreSales);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "BTBDTO":
                                                         //        var btb = new BTBDTO();
                                                         //        btb = btb.ReadObject(localpath);
                                                         //        var btbServices = new BTBServices();
                                                         //        var BTB = btbRepository.GetDataOne(x => x.NoBtb == btb.NoBtb);
                                                         //        //if (BTB == null)
                                                         //        //{
                                                         //        btbServices.Save(btb);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "BtbElseDTO":
                                                         //        var btbElse = new BtbElseDTO();

                                                         //        btbElse = btbElse.ReadObject(localpath);
                                                         //        WCFCache.Current.Insert(btbElse.NoBtb, btbElse);
                                                         //        result = btbElse.NoBtb;
                                                         //        //var btbElseServices = new BtbElseServices();
                                                         //        //    btbElseServices.Save(btbElse, out err);
                                                         //        //ftp.DeleteFile(item.Name););

                                                         //        break;
                                                         //    case "DeliveryOrderDTO":
                                                         //        var DO = new DeliveryOrderDTO();
                                                         //        DO = DO.ReadObject(localpath);
                                                         //        var deliveryOrderServices = new DeliveryOrderServices();
                                                         //        var deliveryOrder = DORepository.GetDataOne(x => x.DONo == DO.DONo);
                                                         //        //if (deliveryOrder == null)
                                                         //        //{
                                                         //        deliveryOrderServices.Save(DO);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "MutasiPalletDTO":
                                                         //        var MutasiPallet = new MutasiPalletDTO();
                                                         //        MutasiPallet = MutasiPallet.ReadObject(localpath);
                                                         //        var mutasiPalletServices = new MutasiPalletServices();
                                                         //        mutasiPalletServices.Save(MutasiPallet);
                                                         //        break;
                                                         //    case "MutasiWarehouseDTO":
                                                         //        var mutasiWarehouse = new MutasiWarehouseDTO();
                                                         //        mutasiWarehouse = mutasiWarehouse.ReadObject(localpath);
                                                         //        var mutasiWarehouseServices = new MutasiWarehouseServices();
                                                         //        var mw = mutasiWarehouseRepository.GetDataOne(x => x.NoMutasi == mutasiWarehouse.NoMutasi);
                                                         //        //if (mw == null)
                                                         //        //{
                                                         //        mutasiWarehouseServices.Save(mutasiWarehouse);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "InventoryAdjustmentDTO":
                                                         //        var inventoryAdjustment = new InventoryAdjustmentDTO();
                                                         //        inventoryAdjustment = inventoryAdjustment.ReadObject(localpath);
                                                         //        var inventoryAdjustmentServices = new InventoryAdjustmentServices();
                                                         //        var invAdjust =
                                                         //            inventoryAdjustRepository.GetDataOne(x => x.NoAdj == inventoryAdjustment.NoAdj);
                                                         //        //if (invAdjust == null)
                                                         //        //{
                                                         //        inventoryAdjustmentServices.Save(inventoryAdjustment);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "PurchaseReturnDTO":
                                                         //        var PR = new PurchaseReturnDTO();
                                                         //        PR = PR.ReadObject(localpath);
                                                         //        var purchaseReturnServices = new PurchaseReturnServices();
                                                         //        var purchaseReturn = prRepository.GetDataOne(x => x.PRNo == PR.PRNo);
                                                         //        //if (purchaseReturn == null)
                                                         //        //{
                                                         //        purchaseReturnServices.Save(PR);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "SalesReturnDTO":
                                                         //        var SR = new SalesReturnDTO();
                                                         //        SR = SR.ReadObject(localpath);
                                                         //        var salesReturnServices = new SalesReturnServices();
                                                         //        var salesReturn = srRepository.GetDataOne(x => x.SRNo == SR.SRNo);
                                                         //        //if (salesReturn == null)
                                                         //        //{
                                                         //        salesReturnServices.Save(SR);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "PurchaseOrderDTO":
                                                         //        var PO = new PurchaseOrderDTO();
                                                         //        PO = PO.ReadObject(localpath);
                                                         //        var purchaseOrderServices = new PurchaseOrderServices();
                                                         //        var purchaseOrder = poRepository.GetDataOne(x => x.PONumber == PO.PONumber);
                                                         //        //if (purchaseOrder == null)
                                                         //        //{
                                                         //        purchaseOrderServices.Save(PO);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "SalesOrderDTO":
                                                         //        var SO = new SalesOrderDTO();
                                                         //        SO = SO.ReadObject(localpath);
                                                         //        var salesOrderServices = new SalesOrderServices();
                                                         //        var salesOrder = soRepository.GetDataOne(x => x.SONo == SO.SONo);
                                                         //        //if (salesOrder == null)
                                                         //        //{
                                                         //        salesOrderServices.Save(SO);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "SpkDTO":
                                                         //        var SPK = new SpkDTO();
                                                         //        SPK = SPK.ReadObject(localpath);
                                                         //        var spkServices = new SpkServices();
                                                         //        var spk = spkRepository.GetDataOne(x => x.NoSPK == SPK.NoSPK);
                                                         //        //if (spk == null)
                                                         //        //{
                                                         //        spkServices.Save(SPK);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        //}
                                                         //        break;
                                                         //    case "ItemDTO":
                                                         //        var itemDto = new ItemDTO();
                                                         //        itemDto = itemDto.ReadObject(localpath);
                                                         //        var ItemServices = new ItemServices();
                                                         //        ItemServices.Save(itemDto);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        break;
                                                         //    case "SubGroupDTO":
                                                         //        var subGroupDto = new SubGroupDTO();
                                                         //        subGroupDto = subGroupDto.ReadObject(localpath);
                                                         //        var SubgroupServices = new SubGroupService();
                                                         //        SubgroupServices.Save(subGroupDto);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        break;
                                                         //    case "GroupDTO":
                                                         //        var groupDto = new GroupDTO();
                                                         //        groupDto = groupDto.ReadObject(localpath);
                                                         //        var GroupServices = new GroupServices();
                                                         //        GroupServices.Save(groupDto);
                                                         //        ftp.DeleteFile(item.Name);
                                                         //        break;
                                                         //}




                                                         //FileInfo fi = new FileInfo(localpath);
                                                         //fi.Delete();
                    ftp.DeleteFile(item.Name);                                    
                    break;
                       
                }

                return Result;
            }
            catch (Exception ex)
            {
                throw ex;
            }finally
            {
                ftp.Close();
            }
           
        }



        public bool  ReceiveTrue(string subject,string localpath,out String errmessage)
        {
            bool res = true ;
            errmessage = string.Empty;
            try
            {
                switch (subject)
                {
                    case "PreSalesOrderDTO":
                        var PreSales = new PreSalesOrderDTO();
                        PreSales = PreSales.ReadObject(localpath);
                        var preSalesOrderServices = new PreSalesOrderServices();
                        var PS = preSalesOrderRepository.GetDataOne(x => x.PSNo == PreSales.PSNo);
                        //if (PS == null)
                        //{
                        preSalesOrderServices.Save(PreSales);
                        //}
                        break;
                    case "BTBDTO":
                        var btb = new BTBDTO();
                        btb = btb.ReadObject(localpath);
                        var btbServices = new BTBServices();
                        var BTB = btbRepository.GetDataOne(x => x.NoBtb == btb.NoBtb);
                        //if (BTB == null)
                        //{
                        btbServices.Save(btb);
                        //}
                        break;
                    case "BtbElseDTO":
                        var btbElse = new BtbElseDTO();
                        btbElse = btbElse.ReadObject(localpath);
                        var btbElseServices = new BtbElseServices();
                        btbElseServices.Save(btbElse);
                        break;
                    case "DeliveryOrderDTO":
                        var DO = new DeliveryOrderDTO();
                        DO = DO.ReadObject(localpath);
                        var deliveryOrderServices = new DeliveryOrderServices();
                        var deliveryOrder = DORepository.GetDataOne(x => x.DONo == DO.DONo);
                        //if (deliveryOrder == null)
                        //{
                        deliveryOrderServices.Save(DO);
                        //}
                        break;
                    case "MutasiPalletDTO":
                        //var MutasiPallet = new MutasiPalletDTO();
                        //MutasiPallet = MutasiPallet.ReadObject(localpath);
                        //var mutasiPalletServices = new MutasiPalletServices();
                        //mutasiPalletServices.Save(MutasiPallet);
                        //break;
                    case "MutasiWarehouseDTO":
                        var mutasiWarehouse = new MutasiWarehouseDTO();
                        mutasiWarehouse = mutasiWarehouse.ReadObject(localpath);
                        var mutasiWarehouseServices = new MutasiWarehouseServices();
                        var mw = mutasiWarehouseRepository.GetDataOne(x => x.NoMutasi == mutasiWarehouse.NoMutasi);
                        //if (mw == null)
                        //{
                        mutasiWarehouseServices.Save(mutasiWarehouse);
                        //}
                        break;
                    case "InventoryAdjustmentDTO":
                        var inventoryAdjustment = new InventoryAdjustmentDTO();
                        inventoryAdjustment = inventoryAdjustment.ReadObject(localpath);
                        var inventoryAdjustmentServices = new InventoryAdjustmentServices();
                        var invAdjust =
                            inventoryAdjustRepository.GetDataOne(x => x.NoAdj == inventoryAdjustment.NoAdj);
                        //if (invAdjust == null)
                        //{
                        inventoryAdjustmentServices.Save(inventoryAdjustment);
                        //}
                        break;
                    case "PurchaseReturnDTO":
                        var PR = new PurchaseReturnDTO();
                        PR = PR.ReadObject(localpath);
                        var purchaseReturnServices = new PurchaseReturnServices();
                        var purchaseReturn = prRepository.GetDataOne(x => x.PRNo == PR.PRNo);
                        //if (purchaseReturn == null)
                        //{
                        purchaseReturnServices.Save(PR);
                        //}
                        break;
                    case "SalesReturnDTO":
                        var SR = new SalesReturnDTO();
                        SR = SR.ReadObject(localpath);
                        var salesReturnServices = new SalesReturnServices();
                        var salesReturn = srRepository.GetDataOne(x => x.SRNo == SR.SRNo);
                        //if (salesReturn == null)
                        //{
                        salesReturnServices.Save(SR);
                        //}
                        break;
                    case "PurchaseOrderDTO":
                        var PO = new PurchaseOrderDTO();
                        PO = PO.ReadObject(localpath);
                        var purchaseOrderServices = new PurchaseOrderServices();
                        var purchaseOrder = poRepository.GetDataOne(x => x.PONumber == PO.PONumber);
                        //if (purchaseOrder == null)
                        //{
                        purchaseOrderServices.Save(PO);
                        //}
                        break;
                    case "SalesOrderDTO":
                        var SO = new SalesOrderDTO();
                        SO = SO.ReadObject(localpath);
                        var salesOrderServices = new SalesOrderServices();
                        var salesOrder = soRepository.GetDataOne(x => x.SONo == SO.SONo);
                        //if (salesOrder == null)
                        //{
                        salesOrderServices.Save(SO);
                        //}
                        break;
                    case "SpkDTO":
                        var SPK = new SpkDTO();
                        SPK = SPK.ReadObject(localpath);
                        var spkServices = new SpkServices();
                        var spk = spkRepository.GetDataOne(x => x.NoSPK == SPK.NoSPK);
                        //if (spk == null)
                        //{
                        spkServices.Save(SPK);
                        //}
                        break;
                    case "ItemDTO":
                        var itemDto = new ItemDTO();
                        itemDto = itemDto.ReadObject(localpath);
                        var ItemServices = new ItemServices();
                        ItemServices.Save(itemDto);
                        break;
                    case "SubGroupDTO":
                        var subGroupDto = new SubGroupDTO();
                        subGroupDto = subGroupDto.ReadObject(localpath);
                        var SubgroupServices = new SubGroupService();
                        SubgroupServices.Save(subGroupDto);
                        break;
                    case "GroupDTO":
                        var groupDto = new GroupDTO();
                        groupDto = groupDto.ReadObject(localpath);
                        var GroupServices = new GroupServices();
                        GroupServices.Save(groupDto);
                        break;
                }
               

            }catch(Exception ex)
            {
                errmessage = ex.Message;
                res = false;
            }finally
            {
                FileInfo fi = new FileInfo(localpath);
                fi.Delete();
            }

            return res;
        }

        
    }
}
   
